package project.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class MailController {
    @Value("${spring.mail.username}")
    private String from;
    /*@Value("${random.int(1000)}")
     private int code;*/
    @Autowired
    private JavaMailSender javaMailSender;
    /*@Resource
    private StringRedisTemplate stringRedisTemplate;*/
    @RequestMapping("/send")
    public String send(HttpServletRequest request, Map map)
    {
        int code=(int)(Math.random()*(10000-1000+1))+1000;
        System.out.println(code);
        HttpSession session = request.getSession();
        if(request.getParameter("username")!=null)
        {
            String username=request.getParameter("username");
            String password=request.getParameter("password");
            session.setAttribute("password",password);
            session.setAttribute("username",username);
            session.setAttribute("code",code);
            SimpleMailMessage message=new SimpleMailMessage();//封装javamail简单四要素
            message.setFrom(from);
            message.setTo(username);
            message.setSubject("注册验证码");
            message.setText("尊敬的用户您好，您的注册验证码为"+code);
            javaMailSender.send(message);
            return "redirect:prove";
        }
        else{
            map.put("error","您还未输入您的邮箱号");
            return "register";
        }


    }
}
